/**
 * Sync manager for Delicious.
 */

var RequestManager = {

    performRequest : function(userAuthToken, requestUrl, callback) {
        //Sleep for second not to be throttled.
        sleep(1000);
        $.ajax({
            'url': requestUrl,

            'beforeSend': function(xhr) {
                xhr.setRequestHeader("Authorization", "Basic " + userAuthToken);
            },

            success: function(result) {
                callback(result);
            }
        });
    }
};

var Delicious = {

    THROTTLED_STATUS_CODE : "999",

    getTagsEndpointUrl : function() {
        return "https://api.del.icio.us/v1/tags/get?"
    },

    getPostsEndpointUrl : function() {
        return "https://api.del.icio.us/v1/posts/all?"
    },

    buildRequestPostsByTagRequestUrl : function(tags) {
        var requestParams = "";

        for (var i = 0; i < tags.length; i++) {
            requestParams += ("tag=" + tag[i]);
            if (i != (tags.length - 1)) {
                requestParams += "?";
            }
        }

        return this.getPostsEndpointUrl() + requestParams;
    },

    fetchPostsByTag : function(userAuthToken, callback, tag) {
        RequestManager.performRequest(userAuthToken, tag, callback);
        /*$.ajax({
            'url': this.getTagsEndpointUrl() + tag,

            'beforeSend': function(xhr) {
                xhr.setRequestHeader("Authorization", "Basic " + userAuthToken);
            },

            success: function(result) {
                callback(result);
            }
        });*/
    },

    fetchTags : function(userAuthToken, callback) {
        RequestManager.performRequest(userAuthToken, 'https://api.del.icio.us/v1/tags/get', callback);
        /*$.ajax({
            'url': 'https://api.del.icio.us/v1/tags/get',

            'beforeSend': function(xhr) {
                xhr.setRequestHeader("Authorization", "Basic " + userAuthToken);
            },

            success: function(result) {
                parse(result);
            }
        });*/
    }
};


//Bookmarks sync manager
var SyncManager = {

    sync : function() {

    },

    scheduleSync : function() {
        //TODO Implement.
    }


};

function parsePosts(result, tag) {
    $("#tag_" + tag + "_content").html("");
    var i = 0;
    $(result).find('post').each(
            function() {
                i++;
                var href = $(this).attr('href');
                var description = $(this).attr('description');
                $("#tag_" + tag + "_content").append(
                        "<div><a id='id" + i + "'href='" + href + "'>"
                                + description + "</a></div>")
                $("#id" + i).click(function() {
                    chrome.tabs.create({
                        'url' : href
                    }, function() {
                    });
                });
            });
};

